<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['workflow:todo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="todoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="流程版本" align="center" prop="processDefinitionVersion"/>
      <el-table-column label="流程实例编号" align="center" prop="processInstanceId"/>
      <el-table-column label="流程部署编号" align="center" prop="deploymentId"/>
      <!--
      <el-table-column label="流程分类" align="center" prop="varMap.auditType"/>
       -->
      <el-table-column label="流程KEY" align="center" prop="processDefinitionKey"/>
      <el-table-column label="流程名称" align="center" prop="processDefinitionName"/>
      <el-table-column label="任务编号" align="center" prop="taskId"/>
      <el-table-column label="节点名称" align="center" prop="name"/>
      <el-table-column label="任务到达时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleCompleteTask(scope.row)"
            v-hasPermi="['workflow:todo:completeTask']"
          >审核</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleQuery(scope.row)"
            v-hasPermi="['workflow:todo:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleTerminateProcess(scope.row)"
            v-hasPermi="['workflow:todo:terminateProcess']"
          >终止流程</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看任务对话框 -->
    <el-dialog :title="title" :visible.sync="viewTaskOpen" width="500px" append-to-body>
      <el-card class="box-card">
          <div slot="header" class="clearfix">
            <span>任务详情</span>
          </div>
          <div class="text item">当前环节:{{this.formView.name}}</div>
          <div class="text item">任务编号:{{this.formView.taskId}}</div>
          <div class="text item">任务到达时间:{{this.formView.createTime}}</div>
          <div class="text item">任务发起人:{{this.formView.varMap.userMap.applyUserName}}</div>
      </el-card>


       <el-card class="box-card" style="margin-top:10px">
          <div slot="header" class="clearfix">
            <span>业务详情</span>
          </div>
          <div class="text item">文档编号:{{this.formView.varMap.businessMap.contentId}}</div>
          <div class="text item">文档名称:{{this.formView.varMap.businessMap.contentName}}</div>
          <div class="text item">文档类型:{{this.formView.varMap.businessMap.contentTypeId}}</div>
          <!-- <div class="text item">文档描述:{{this.formView.varMap.businessMap.description}}</div> -->
          <div class="text item">文档状态:{{this.formView.varMap.businessMap.statusId}}</div>
          <div class="text item">版本号:{{this.formView.varMap.businessMap.version}}</div>
      </el-card>
      <el-card class="box-card" style="margin-top:10px">
          <div slot="header" class="clearfix">
            <span>审批历史</span>
          </div>
          <el-timeline>
            <el-timeline-item
              v-for="(activity, index) in activities"
              :key="index"
              :icon="activity.icon"
              :type="activity.type"
              :color="activity.color"
              :size="activity.size"
              :timestamp="activity.timestamp">
              {{activity.content}}
              <div v-if="activity.timestamp!=null">审批意见：{{activity.taskMessage}}</div>
              <div v-if="activity.timestamp!=null">备注：{{activity.taskComment}}</div>
            </el-timeline-item>
          </el-timeline>
      </el-card>
      
      <div slot="footer" class="dialog-footer">
        <el-button @click="viewTaskOpen=false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 审批任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="任务编号" prop="taskId">
          <el-input v-model="form.taskId" placeholder="请输入任务编号" readonly/>
        </el-form-item>
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入任务编号" readonly/>
        </el-form-item>
        <el-form-item label="文档名称" prop="contentName">
          <el-input v-model="form.contentName" placeholder="请输入文档名称" readonly/>
        </el-form-item>
        <el-form-item label="审批标志" prop="taskStatus">
          <el-select v-model="form.taskStatus" placeholder="请选择审批标志">
            <el-option
              v-for="status in taskStatusOptions"
              :key="status.statusValue"
              :label="status.statusLabel"
              :value="status.statusValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注">
          <el-input type="textarea" v-model="form.taskComment" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 终止流程对话框 -->
    <el-dialog :title="title" :visible.sync="terminateOpen" width="500px" append-to-body>
      <el-form ref="terminateForm" :model="terminateForm" :rules="rules" label-width="80px">
        <el-form-item label="终止原因" prop="deleteReason">
          <el-input type="textarea" v-model="terminateForm.deleteReason" placeholder="请输入终止原因" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitTerminateForm">确 定</el-button>
        <el-button @click="terminateOpen=false">取 消</el-button>
      </div>
    </el-dialog>

    
  </div>
</template>
<style scoped>
/*1.显示滚动条：当内容超出容器的时候，可以拖动：*/
.el-drawer__body {
   overflow: auto;
}

</style>
<script>
import { listTodo, getTodo, completeTask, exportTodo,queryHistoryTask,terminateProcess } from "@/api/workflow/todo";

export default {
  name: "Todo",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 待办任务表格数据
      todoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示查看弹出层
      viewTaskOpen:false,
      // 是否显示终止弹出层
      terminateOpen:false,
      // 审批标志字典
      taskStatusOptions: [
        {
          "statusLabel":"同意",
          "statusValue":"agree",
        },{
          "statusLabel":"拒绝",
          "statusValue":"reject",
        }
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 查看表单参数
      formView:{
        varMap:{
          userMap:{},
          businessMap:{}
        }
      },
      // 终止流程表单参数
      terminateForm: {

      },
      // 表单校验 对应的是el-form-item的prop
      rules: {
        taskId: [
          { required: true, message: "任务编号不能为空", trigger: "blur" }
        ],
        taskStatus: [
          { required: true, message: "审批标志不能为空", trigger: "blur" }
        ],
        deleteReason: [
          { required: true, message: "终止原因不能为空", trigger: "blur" }
        ],
      },
      activities: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询待办任务列表 */
    getList() {
      this.loading = true;
      listTodo(this.queryParams).then(response => {
        this.todoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        taskId: null,
        name: null,
        auditType: null,
        processInstanceId: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.taskId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 审核任务按钮操作 */
    handleCompleteTask(row) {
      this.form.taskId = row.taskId;
      this.form.name = row.name;
      this.form.contentName = row.varMap.businessMap.contentName;
      this.title = "审核待办任务";
      this.open = true;
    },
    /** 终止流程按钮操作 */
    handleTerminateProcess(row) {
      this.terminateForm.processInstanceId = row.processInstanceId;
      this.title = "终止流程";
      this.terminateOpen = true;
    },
    

    /** 查看任务按钮操作 */
    handleQuery(row) {
      let that = this;
      this.viewTaskOpen = true;
      this.title = "查看待办任务";
      this.formView = row;
      queryHistoryTask(row.processInstanceId).then(response => {
        if(response && response.data){
          that.activities = [];
          response.data.forEach(function(item) {
            let activitie = {};
            if(activitie.assigneeName == null || activitie.assigneeName == ""  || activitie.assigneeName == undefined){
              activitie.assigneeName = item.assignee;
            }
            activitie.content = "[" + item.name + "] " + item.assigneeName;
            activitie.timestamp= item.endTime;
            activitie.taskMessage = item.taskMessage;
            activitie.taskComment = item.taskComment;
            activitie.approveTime = item.approveTime;
            activitie.taskStatus = item.taskStatus;
            // activitie.size = 'large';
            // activitie.type = 'primary';
            // activitie.icon = 'el-icon-more';
            if(item.endTime==null){
                 activitie.color='#0bbd87';
                 activitie.content = item.name;
            } else if(item.taskStatus=="reject"){
                 activitie.color='#DC143C';
            }
            that.activities.push(activitie);
          }); 
        }
        let activitie = {};
      });
      // const taskId = row.taskId || this.ids
      
    },
    /** 提交审核按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.taskId != null) {
            completeTask(this.form).then(response => {
              this.msgSuccess("审核成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.$message.error('任务不存在！')
          }
        }
      });
    },

    /** 提交终止流程按钮 */
    submitTerminateForm() {
      this.$refs["terminateForm"].validate(valid => {
        if (valid) {
          if (this.terminateForm.processInstanceId != null) {
            terminateProcess(this.terminateForm).then(response => {
              this.msgSuccess("流程终止成功");
              this.terminateOpen = false;
              this.getList();
            });
          } else {
            this.$message.error('流程编号不存在！')
          }
        }
      });
    },

    
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有待办任务数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportTodo(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>

